//
//  CTPickImageView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/15.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPickImageView.h"
#import "CTPickImageCell.h"

#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTDeviceFunction.h"
#import "CTAppLanguage.h"
#import "CTRemindView.h"
#import "CTPickAlbumView.h"
#import "CTTextManager.h"
#import "UIViewAdditions.h"

typedef enum {
    CTPickAlbumViewAnimationShow = 999,
    CTPickAlbumViewAnimationHide
}CTPickAlbumViewAnimation;

@interface CTPickImageView ()<UICollectionViewDataSource,UICollectionViewDelegate,CTPickAlbumViewDelegate>
{
    UICollectionView * pickImageCollectionView;
    CTPickAlbumView * albumPickView ;
    UIButton *albumPickBtn;
    
    UIView * topView;
    UILabel *titleLabel;
    
    NSArray * imagInfoArray;
    NSArray * imagInfoFullScreenArray;
    
    NSString * pickedImgUrl;
   
    NSInteger cellIndex;
}

@end


@implementation CTPickImageView
@synthesize delegate;


- (void)dealloc
{
    [albumPickBtn removeFromSuperview];
    albumPickBtn = nil;
    
    [topView removeFromSuperview];
    topView = nil;
    
    [pickImageCollectionView removeFromSuperview];
    pickImageCollectionView = nil;
    
    [albumPickView removeFromSuperview];
    albumPickView.delegate = self;
    albumPickView = nil;
    
    imagInfoArray = nil;
    imagInfoFullScreenArray = nil;
    delegate = nil;
    
    [titleLabel removeFromSuperview];
    titleLabel = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPickImageView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        cellIndex = -1;
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
       
        [self ctreatSubviews];
        
    }
    return self;
}

#pragma mark - creat subViews -
-(void)ctreatSubviews
{
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    pickImageCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 44, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-44) collectionViewLayout:flowLayout];
    pickImageCollectionView.delegate = self;
    pickImageCollectionView.dataSource = self;
    [self addSubview:pickImageCollectionView];
    pickImageCollectionView.backgroundColor = [UIColor clearColor];
    [pickImageCollectionView registerClass:[CTPickImageCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    [self creatAlbumPickView];
    
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:21];
    [topView addSubview:titleLabel];
    
    albumPickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    albumPickBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-100)/2.0, 20,100,44);
    albumPickBtn.tag = CTPickAlbumViewAnimationShow;
    albumPickBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    [albumPickBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [albumPickBtn addTarget:self action:@selector(pickAlbumViewBtn:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:albumPickBtn];
    
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(cancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"quxiao"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"quxiao_click"] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
//    UIButton * confirmBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    confirmBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-60, 20, 55, 44);
//    [confirmBtn addTarget:self action:@selector(nextStepBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [confirmBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
//    [confirmBtn setTitle:[CTAppLanguage localizedStringForKey:@"pickImageView_topView_confirm" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
//    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//    [topView addSubview:confirmBtn];
    
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
    
    
 
}

-(void)creatAlbumPickView
{
    albumPickView = [[CTPickAlbumView alloc] init];
    albumPickView.delegate = self;
    [self addSubview:albumPickView];
}



#pragma mark - action delegate -
-(void)cancelBtn:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(cancelAction:)]) {
        [self.delegate cancelAction:self];
    }
}

-(void)nextStepBtn:(id)sender
{
    if ([[CTTextManager shareInstance] isBlankString:pickedImgUrl] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_pickedImg" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(nextStepAction:withImg:)]) {
        [self.delegate nextStepAction:self withImg:pickedImgUrl];
    }
}

-(void)pickAlbumViewBtn:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    
    if (btn.tag == CTPickAlbumViewAnimationShow) {
        btn.tag = CTPickAlbumViewAnimationHide;
        
        [UIView animateWithDuration:0.3f animations:^{
            CGRect rect = albumPickView.frame;
            rect.origin.y += CT_HEADER_SCREEN_HEIGHT-136;
            albumPickView.frame = rect;
        } completion:^(BOOL finished) {
            
        }];
        
    }else{
        btn.tag = CTPickAlbumViewAnimationShow;
        
        [UIView animateWithDuration:0.3f animations:^{
            CGRect rect = albumPickView.frame;
            rect.origin.y -= CT_HEADER_SCREEN_HEIGHT-136;
            albumPickView.frame = rect;
        } completion:^(BOOL finished) {
            
        }];
    }
    
}


#pragma mark - CTPickAlbumViewDelegate -

-(void)albumPickedAction:(CTPickAlbumView *)view withItem:(CTAlbumItem *)itm
{
    albumPickBtn.tag = CTPickAlbumViewAnimationShow;
    [albumPickBtn setTitle:itm.groupName forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rect = albumPickView.frame;
        rect.origin.y -= CT_HEADER_SCREEN_HEIGHT-136;
        albumPickView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
    
    if ([self.delegate respondsToSelector:@selector(albumPickedViewShowAction:withItem:)]) {
        [self.delegate albumPickedViewShowAction:self withItem:itm];
    }
}

-(void)albumPickViewHiddenAction:(CTPickAlbumView *)view
{
    albumPickBtn.tag = CTPickAlbumViewAnimationShow;
    
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rect = albumPickView.frame;
        rect.origin.y -= CT_HEADER_SCREEN_HEIGHT-136;
        albumPickView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [imagInfoArray count];
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"cell";
    
    CTPickImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
    
    
    cell.isPicked = NO;
    cell.backgroundColor = [UIColor blueColor];
    cell.img = [imagInfoArray objectAtIndex:indexPath.row];
    

    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((CT_HEADER_SCREEN_WIDTH-6)/3, (CT_HEADER_SCREEN_WIDTH-6)/3);
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3,0,3,0);
}
//定义UICollectionView 每个item的纵向间距的
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 3;
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([self.delegate respondsToSelector:@selector(cameraAction:)]) {
            [self.delegate cameraAction:self];
        }
    }else{
        
        pickedImgUrl = [NSString stringWithFormat:@"%@",(NSString *)[imagInfoFullScreenArray objectAtIndex:indexPath.row]];
        
        
        if ([[CTTextManager shareInstance] isBlankString:pickedImgUrl] == YES) {
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_pickedImg" withValue:nil withTable:@"Language"]];
            return;
        }
        if ([self.delegate respondsToSelector:@selector(nextStepAction:withImg:)]) {
            [self.delegate nextStepAction:self withImg:pickedImgUrl];
        }
        
    }
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



#pragma mark - setter -
-(void)setImageArray:(NSArray *)imgArray{
    imagInfoArray = imgArray;
    [pickImageCollectionView reloadData];
}
-(void)setImageFullScreenArray:(NSArray *)imgFullScreenArray
{
    imagInfoFullScreenArray = imgFullScreenArray;
}
-(void)setAlbumArray:(NSArray *)albumArray{
    
    CTAlbumItem * item;
    if (albumArray.count != 0){
        item = (CTAlbumItem *)[albumArray objectAtIndex:0];
        [albumPickBtn setTitle:item.groupName forState:UIControlStateNormal];
    }
    
    albumPickView.albumArray = albumArray;
}

@end
